const express=require('express');
const mysql=require('mysql');
//引用自定义
const commte=require('../../libs/common.js');
const urlobj=require('url');
const multer=require('multer');
const multerObj=multer({dest:'../../www/upload'});
const pathLib=require('path');
const fs=require('fs');
//node request模块安装命令：npm install request
var request = require('request');
var querystring = require('querystring');
//链接数据库
var db=mysql.createPool({
    host:'localhost',
    user:'root',
    password:'',
    database:'leasedb'
});;
module.exports = function() {
	var router = express.Router();

    router.post('/indexlogin',function(req,res){ 
    db.query("SELECT * FROM `userinfo` WHERE userName='"+req.body.userName+"'",function (err,data) {
        if(err)
        {
        	//程序出错
            res.send('{"status":"400"}').end();

        }else
        {
            if(data.length==0)
            {
            	//没有查询到用户名
                res.send('{"status":"300","msg":'+JSON.stringify(data)+'}').end();
            }else
            {
                db.query("SELECT * FROM `userinfo` WHERE userName='"+req.body.userName+"' and userPwd='"+req.body.userPwd+"'",function(err,data) {
                    if (err)
                    {
                        res.status(500).send('<script>alert("出错了!");window.location.href="404.html";</script>').end();
                    }else
                    {
                        if(data.length==0)
                        {//没有查询到用户名密码
                            //程序出错
                            res.send('{"status":"302"}').end();
                        }else
                        {

                            res.send('{"status":"200","msg":'+JSON.stringify(data)+'}').end();
                        }
                    }
                })
            }
        }
    }) ;
    }) ;
    
    
	//用户注册
	router.use('/register', function(req, res) {
		
//	console.log(common.md5(req.body.userPwd))
		var userName = req.body.userName;
		var userPwd = req.body.userPwd; 
		var usersjs=req.body.usersjs; 
			   var Nickname="";
           			 for(var i=0;i<6;i++) {
                		Nickname+= Math.floor(Math.random()*10); 
            		}   
		var Nickname=("XYXY"+Nickname)
		 
	 
		//查询当前注册名是否已存在
	db.query("SELECT * FROM `userinfo` WHERE userName='"+userName+"'", function(err, data) {
				if (err) {
					res.send('{"status":"400"}').end();
				} else {
					if (data.length==1) {
						//用户名已存在
						res.send('{"status":"600"}').end();
					} else {
						db.query("INSERT INTO `userinfo`(`userId`, `Nickname`, `userImg`, `userName`, `userPwd`, `usertime`) VALUES (0,'"+Nickname+"','banner1.jpg','"+userName+"','"+userPwd+"','"+commte.getTime()+"')", function(err, data) {
							if (err) {
								res.send('{"status":"400"}').end();
							} else { 
								//注册成功
								res.send('{"status":"200","msg":' + JSON.stringify(data) + '}').end();
							}
						});
					}
				}
			});	 
	});
	

router.use('/ma', function(req, res) {
      console.log("我的验证码"+req.body.VerificationCode);
 
		var queryData = querystring.stringify({
		    "mobile": ""+req.body.phone+"",  // 接受短信的用户手机号码
		    "tpl_id": 115808,  // 您申请的短信模板ID，根据实际情况修改
		    "tpl_value": "#code#="+req.body.VerificationCode+"",  // 您设置的模板变量，根据实际情况修改
		    "key": "5d654d0e811db54348749dd2342770d0",  // 应用APPKEY(应用详细页查询)
		});
		var queryUrl = 'http://v.juhe.cn/sms/send?'+queryData;
		request(queryUrl, function (error, response, body) {
			if (!error && response.statusCode == 200) {
				console.log(body) // 打印接口返回内容
				var jsonObj = JSON.parse(body); // 解析接口返回的JSON内容
				console.log(jsonObj)
			} else {
				console.log('请求异常');
			}
		}); 
});



	
	//生成验证码
router.use('/VerificationCode', function(req, res) {
    			   var sjs="";
           			 for(var i=0;i<6;i++) {
                		sjs+= Math.floor(Math.random()*10); 
            		}  
 res.send('{"status":"200","msg":'+JSON.stringify(sjs)+'}').end();
});


	return router;
}
